package com.lilosoft.core.utils;

import com.lilosoft.core.utils.commons.DateUtils;
import oracle.sql.TIMESTAMP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字符串工具类, 继承com.lilosoft.common.utils.commons.StringUtils类
 * @author qiusheng
 *
 */
public final class StringUtils extends com.lilosoft.core.utils.commons.StringUtils {
	
	/**
	 * 转换事项资料为字符串
	 * @param clob
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
    public static String ClobToString(Clob clob) throws SQLException, IOException {   
        if(clob == null) {   
            return null;   
        }   
        try  
        {   
            Reader inStreamDoc = clob.getCharacterStream();   
               
            char[] tempDoc = new char[(int) clob.length()];   
            inStreamDoc.read(tempDoc);   
            inStreamDoc.close();   
            return new String(tempDoc);   
        }   
        catch (IOException e)   
        {   
        	e.printStackTrace();   
        }   
        catch (SQLException es)   
        {   
        	es.printStackTrace();   
        }   
             
        return null;   
	}
	
	/**
     * 获取当前系统时间yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentTimeString() {
    	return DateUtils.getDateTime();
    }
    
    /**
     * 获取当前系统时间yyyy-MM-dd
     * @return
     */
    public static String getCurrentDateString() {
    	return DateUtils.getDate();
    }
    
    /**
	 * Clob对象转换为String
	 * @param clob CLOB
	 * @return String
	 * @throws SQLException Exception
	 */
	public static String clobToString(Clob clob){
		String result="";
		try {
			if (clob instanceof Clob) {
				result=(clob != null ? clob.getSubString(1, (int) clob.length()) : null);
			}
		} catch (Exception e) {
			
		}
		return result;
	}
	
	public static List<HashMap<String,Object>> listMapKeyToLowerCase(List<HashMap<String,Object>> list) {
		List<HashMap<String,Object>> tmpList = new ArrayList<HashMap<String,Object>>();
		if(list != null && list.size() > 0){
			for(HashMap<String,Object> map:list){
				tmpList.add(mapKeyToLowerCase(map));
			}
			list = tmpList;
		}
		return tmpList;
	}
	
	public static HashMap<String,Object> mapKeyToLowerCase(HashMap<String,Object> map) {
		HashMap<String,Object> tmpMap = new HashMap<String, Object>();
		if(map != null && map.size() > 0){
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey();
				Object value = entry.getValue();
				if(value instanceof com.alibaba.druid.proxy.jdbc.ClobProxyImpl){
					try {
						BufferedReader in = new BufferedReader(((com.alibaba.druid.proxy.jdbc.ClobProxyImpl)value).getCharacterStream());
						StringWriter out = new StringWriter();
						int c;
						while ((c = in.read()) != -1) {
							out.write(c);
						}
						value = out.toString();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}else if(value instanceof Clob){
					try {
						BufferedReader in = new BufferedReader(((Clob)value).getCharacterStream());
						StringWriter out = new StringWriter();
						int c;
						while ((c = in.read()) != -1) {
							out.write(c);
						}
						value = out.toString();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}else if(value instanceof oracle.sql.TIMESTAMP){
					oracle.sql.TIMESTAMP time = (oracle.sql.TIMESTAMP)value;
					value = getDateBySqlTimestamp(time,null);
				}
				tmpMap.put(key.toLowerCase(), value);
			}
		}
		
		return tmpMap;
	}
	
	/**
	 * 根据oracle的Timestamp获取字符串日期时间
	 * 
	 * @param t Timestamp时间
	 * @param formatStr 格式化字符串，如果是null默认yyyy-MM-dd hh:mm:ss
	 * @return 格式化后的字符串
	 */
	public static String getDateBySqlTimestamp(TIMESTAMP time, String formatStr) {
		try {
			if (formatStr == null || "".equals(formatStr)) {
				formatStr = "yyyy-MM-dd hh:mm:ss";
			}
			Timestamp tt;
			tt = time.timestampValue();
			Date date = new Date(tt.getTime());
			
			return DateUtils.formatDate(date, formatStr);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 将map中所有clob类型的字段都转换为string，返回给前端
	 * @param map
	 * @return
	 */
	public static HashMap<String,Object> clobToStringByMap(HashMap<String,Object> map){
		if(map==null){
			return map;
		}
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			Object t1=entry.getValue();
			if(t1 instanceof Clob){//将所有Clob类型的字段都转换为String类型
				entry.setValue(clobToString((Clob)t1));
			}
		}
		return map;
	}
	
	/**
	 * 将List<HashMap<String,Object>>中所有clob类型的字段都转换为string，返回给前端
	 * @param map
	 * @return
	 */
	public static List<HashMap<String,Object>> clobToStringByList(List<HashMap<String,Object>> mapList){
		if(mapList==null){
			return mapList;
		}
		for (HashMap<String,Object> map : mapList) {
			clobToStringByMap(map);
		}
		return mapList;
	}
	
	/**
	 * 将List<String>中信息去重复后转换为String[]
	 * @param strList
	 * @return
	 */
	public static String[] listToStringArr(List<String> strList){
		String[] resultStr={"-1"};
		List<String> resultList=new ArrayList<String>();
		if(strList!=null && strList.size()>0){
			String[] paramStr=(String[])strList.toArray(new String[strList.size()]);
			for(int i=0;i<paramStr.length;i++){
				if(!resultList.contains(paramStr[i])){
					resultList.add(paramStr[i]);
				}
			}
			resultStr=(String[])resultList.toArray(new String[resultList.size()]);
			return resultStr;
		}
		return resultStr;
	}

	/**
	 * 获取批次号
	 * @return
	 */
	public static String getCDBatch(){
		return DateUtils.formatDate(new Date(), "yyyyMMdd")+"00002";
	}
}
